<?php
	
	class stat_shoporder_monthControl extends skymvc{
		public $sw=" k='shop' ";
		public function __construct(){
			
			parent::__construct();
			
		}
		
		public function onDefault(){
			$where= $this->sw." AND  shopid=".SHOPID;
			$start=get('per_page','i');
			$limit=20;
			$url=APPSHOP."?m=stat_shoporder_month";
			$option=array(
				"where"=>$where,
				"start"=>$start,
				"limit"=>$limit,
				"order"=>" id DESC",
			);
			$rscount=true;
			$data=M("stat_shoporder_month")->select($option,$rscount);
			$pagelist=$this->pagelist($rscount,$limit,$url);
			$this->smarty->assign(array(
				"list"=>$data,
				"rscount"=>$rscount,
				"pagelist"=>$pagelist
				
			));
			
			$this->smarty->display("stat_shoporder_month/index.html");
		}
		
		public function onChart(){
			$where= $this->sw." AND  shopid=".SHOPID;
			$start=get('per_page','i');
			$limit=30;
			$url=APPKOUDAI."?m=stat_shoporder_day";
			$option=array(
				"where"=>$where,
				"start"=>$start,
				"limit"=>$limit,
				"order"=>" id DESC",
			);
			$rscount=true;
			$data=M("stat_shoporder_month")->select($option,$rscount);
			$pagelist=$this->pagelist($rscount,$limit,$url);
			$labels="";
			$moneys="";
			if($data){
				
				foreach($data as $k=>$v){
					$labels .=($k==0)?('"'.substr($v['kmonth'],4,4).'"'):(',"'.substr($v['kmonth'],4,4).'"');
					$moneys .=($k==0)?$v['money']:(",".$v['money']);
				}
			}
			 
			$this->smarty->assign(array(
				"list"=>$data,
				"rscount"=>$rscount,
				"pagelist"=>$pagelist,
				"labels"=>$labels,
				"moneys"=>$moneys,
				
			));
			
			$this->smarty->display("stat_shoporder_month/chart.html");
		}
		
		public function onJieSuan(){
			M("stat_shoporder_month")->begin();
			$id=get('id','i');
			$row=M("stat_shoporder_month")->selectRow("id=".$id);
			if(empty($row)){
				$this->goAll("数据出错",1);
			}
			
			if($row['jiesuan']){
				$this->goAll("已结算过",1);
			}
				
			$shop=M("shop")->selectRow("id=".$row['shopid']);
			//夜猫店
			$money1=$row['money']*YM_GL_MMONEY;
			//宣传员
			$money2=$row['money']* YM_XC_MONEY;
			M('shop')->changenum('balance',$money1,"id=".$row['shopid']);
			$nshop=M("shop")->selectRow("id=".$row['shopid']);
			$logdata=array(
				"dateline"=>time(),
				"shopid"=>SHOPID,
				"type_id"=>1,
				"ispay"=>1,
				"money"=>$money1,
				"content"=>"您的店铺".$row['kmonth']."月结算，收入".$money1."元,原来".$shop['balance'].",现在".$nshop['balance']."",
			);
			M('shop_pay_log')->insert($logdata);
			//宣传员
			$tg=M("tgyuan_area")->selectRow("sc_id=".$shop['sc_id']);
			if($tg){
				$tgyuan=M("tgyuan")->selectRow("id=".$tg['tgyid']);
				M('tgyuan')->changenum('balance',$money2,"id=".$tg['tgyid']);
				$ntgyuan=M("tgyuan")->selectRow("id=".$tg['tgyid']);
				$logdata=array(
					"dateline"=>time(),
					"shopid"=>SHOPID,
					"type_id"=>1,
					"ispay"=>1,
					"money"=>$money1,
					"content"=>"夜猫店".$shop['title'].$row['kmonth']."月结算，收入".$money2."元,原来".$tgyuan['balance'].",现在".$ntgyuan['balance']."",
				);
				M('tgyuan_pay_log')->insert($logdata);
			}
			M("stat_shoporder_month")->update(array(
				"jiesuan"=>1
			),"id=".$id);
			M("stat_shoporder_month")->commit();
			$this->goAll("结算成功");
		}
		
			
	}
	
	
?>